package com.yndsoft.jc.ui.memberpage.score;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Member;
import com.yndsoft.jc.model.MemberPoint;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.ui.memberpage.myorder.EvaluateActivity;
import com.yndsoft.jc.ui.memberpage.myorder.OrderDetailActivity;
import com.yndsoft.jc.ui.memberpage.myorder.WholeFragment;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.widget.SlideRecycleView;
import com.yndsoft.jc.widget.SlideRecycleViewAdapter;
import com.yndsoft.jc.widget.SlideViewHolder;
import com.yndsoft.jc.widget.pulltorefresh.PullToRefreshBase;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/7.
 */

public class ScoreDetailActivity extends BaseActivity implements View.OnClickListener, SlideRecycleViewAdapter.OnItemClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.score_detail_list)
    SlideRecycleView score_detail_list;
    @Bind(R.id.point_now)
    TextView point_now;

    private int start = 0;
    private TextView loadAllView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_detail);

        setViews();
        getDatas(RefreshType.PULL_DWON_TO_REFRESH, start);
        setListener();
    }


    int returnCode = 0;
    List<MemberPoint> pointList = new ArrayList<>();

    private void getDatas(final RefreshType type, int start) {
        mHttpUtil.getMemberPoint(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1://还有第二页
                        returnCode = 1;
                        break;
                    case 2://没有第二页
                        returnCode = 2;
                        if (null != score_detail_list) {
                            score_detail_list.onRefreshComplete();
                        }

                        Type type = new TypeToken<List<MemberPoint>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        pointList = (ArrayList<MemberPoint>) fromJson;
                        pointListAdapter.setDatas(pointList);
                        score_detail_list.onRefreshComplete();
                        break;
                }
            }
        }, "pointlist", mSession.getUserInfo().getId(), start, 10);
    }

    @Override
    public void onItemClickListener(int posotion) {

    }


    enum RefreshType {
        PULL_DWON_TO_REFRESH,
        PULL_UP_TO_REFRESH
    }

    PointListAdapter pointListAdapter;

    private void setViews() {
        base_title.setText(getResources().getString(R.string.score_detail));
//        SharedPreferences sp = getSharedPreferences(getString(R.string.sp_member), Context.MODE_PRIVATE);
//        String member = sp.getString(getString(R.string.sp_jc), "");
//        Type type = new TypeToken<Member>() {
//        }.getType();
//        Object fromJson = new Gson().fromJson(member, type);
//        Member mm = (Member) fromJson;
        AppUtils.setText(point_now, mSession.getUserInfo().getPoint() + "");

        score_detail_list.setScrollingWhileRefreshingEnabled(false); //设置刷新的时候不可滑动

        score_detail_list.getRefreshableView().setLayoutManager(new LinearLayoutManager(this)); //添加布局管理器
        score_detail_list.setMode(PullToRefreshBase.Mode.BOTH);
        score_detail_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                loadAllView.setVisibility(View.GONE);
                start = 0;
                getDatas(RefreshType.PULL_DWON_TO_REFRESH, start);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                if (returnCode == 1) {
                    start++;
                    getDatas(RefreshType.PULL_UP_TO_REFRESH, start);
                } else if (returnCode == 2) {
                    loadAllView.setVisibility(View.VISIBLE);
                    score_detail_list.onRefreshComplete();
                }

//                if (haveAnother) {
//
//                } else {
//                    mRecycleView.onRefreshComplete();
//                    mScoreAdapter.addFooterView(loadAllView);
//                }
            }
        });
        loadAllView = new TextView(this);
        loadAllView.setPadding(0, DensityUtil.dip2px(this, 10), 0, DensityUtil.dip2px(this, 10));
        loadAllView.setGravity(Gravity.CENTER);
        loadAllView.setTextSize(15);
        loadAllView.setText("已加载全部");
        pointListAdapter = new PointListAdapter(this, pointList, R.layout.item_score_detail_list);
        loadAllView.setVisibility(View.GONE);
        pointListAdapter.addFooterView(loadAllView);
        pointListAdapter.setOnItemClickListener(this);
        score_detail_list.getRefreshableView().setAdapter(pointListAdapter);
    }

    class PointListAdapter extends SlideRecycleViewAdapter<MemberPoint> {


        /**
         * 构造器
         *
         * @param context
         * @param mDatas
         * @param itemLayoutId
         */
        protected PointListAdapter(Context context, List<MemberPoint> mDatas, int itemLayoutId) {
            super(context, mDatas, itemLayoutId);
        }

        @Override
        public void convert(SlideViewHolder holder, final MemberPoint item) {
            TextView product_appraise_num = holder.findViewById(R.id.product_appraise_num);
            TextView point_time = holder.findViewById(R.id.point_time);
            TextView point_num = holder.findViewById(R.id.point_num);

            AppUtils.setText(product_appraise_num, item.getNote());
            AppUtils.setText(point_time, item.getTime());
            AppUtils.setText(point_num, item.getType() + item.getPoint());
        }

    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
        }
    }
}
